﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIUnionFightBattleResult : MonoBehaviour
    {
        public GameObject winUI;
        public GameObject loseUI;
        public UIUnionFightAwardItem[] awards;
        public UIUnionFightPlayerItem[] bestPerson;
        public UILabel leftName;
        public UILabel rightName;
        public UILabel leftNum;
        public UILabel rightNum;

        public void Init(bool isWin, UFFightResult result)
        {
            gameObject.SetActive(true);
            winUI.SetActive(isWin);
            loseUI.SetActive(!isWin);
            SetUnionName(isWin,result);

            SetAward(result.selfAchieves);
            SetPerson(result.mvpInfos);
        }

        void SetUnionName(bool isWin, UFFightResult result)
        {
            var unions = UIUnionFightRunMap.GetNetData().netBattle.unionInfos;
            string winName = "";
            string loseName = "";
            foreach (var union in unions)
            {
                if (union.unionId == result.winUnionId)
                {
                    winName = string.Format(TableTextBlo.GetText("union_fight_name"), union.serverId, union.unionName);
                }
                else if (union.unionId == result.loseUnionId)
                {
                    loseName = string.Format(TableTextBlo.GetText("union_fight_name"), union.serverId, union.unionName);
                }
            }
            if (isWin)
            {
                leftName.text = "[000000]" + winName + "[-]";
                leftNum.text = "[000000]" + string.Format(TableTextBlo.GetText("union_fight_prestige"), result.winUnionReward) + "[-]";

                rightName.text = "[616161]" + loseName + "[-]";
                rightNum.text = "[616161]" + string.Format(TableTextBlo.GetText("union_fight_prestige"), result.loseUnionReward) + "[-]";
            }
            else
            {
                leftName.text = "[616161]" + loseName + "[-]";
                leftNum.text = "[616161]" + string.Format(TableTextBlo.GetText("union_fight_prestige"), result.loseUnionReward) + "[-]";

                rightName.text = "[000000]" + winName + "[-]";
                rightNum.text = "[000000]" + string.Format(TableTextBlo.GetText("union_fight_prestige"), result.winUnionReward) + "[-]";
            }
        }

        void SetAward(List<UFFightResultSelfAchieve> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                awards[list[i].type].SetItem(list[i].isSuccess,list[i].rewards);
            }
        }

        void SetPerson(List<UFFightResultPlayer> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                bestPerson[list[i].type].SetPlayerItem(list[i]);
            }
        }

        public void OnClickBlank()
        {
            gameObject.SetActive(false);
        }
    }
}

